<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/21
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Controller;

/**
 * Class typeAttr
 * @package app\admin\controller
 * 类型属性控制器
 */
class typeAttr extends Controller
{
    protected $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\TypeAttr();
    }
    //类型属性主页
    public function index(){
        $tid=input('param.tid');
        $data=$this->db->where('tid',$tid)->select();
        $this->assign('data',$data);
        //视图
        return view();
    }
    //类型属性的添加、编辑
    public function add(){
        $tid=input('param.tid');
        $taid=input('param.taid');
        if($taid !== null){
            //编辑
            $oldData=$this->db->where('taid',$taid)->find();
            if(request()->isPost()){
                $data=input('post.');
                $res=$this->db->add($data);
                if ($res['valid']) {
                    $this->success($res['msg'], url('index', ['tid' => $tid]));
                } else {
                    $this->error($res['msg']);
                }
            }
        }else {
            //添加
            $oldData['taname']='';
            $oldData['class']='';
            $oldData['tavalue']='';
            if (request()->isPost()) {
                $data = input('post.');
                $res = $this->db->add($data);
                if ($res['valid']) {
                    $this->success($res['msg'], url('index', ['tid' => $tid]));
                } else {
                    $this->error($res['msg']);
                }
            }
        }
        $this->assign('oldData',$oldData);
        //视图
        return view();
    }

    //删除
    public function del($taid){
        $tid=input('param.tid');
      $res=$this->db->del($taid);
        if ($res['valid']) {
            $this->success($res['msg']);
        } else {
            $this->error($res['msg']);
        }
    }
}